//
//  WebViewController.m
//  VideoTest
//
//  Created by lanou3g on 15/5/29.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//


#import "WebViewController.h"
#import "UMSocial.h"
#import "DatabaseHandle.h"

@interface WebViewController ()<UIWebViewDelegate>
@end


@implementation WebViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadIndicatorView];
    
    [self setBarButtonItems];
    
    [self setWebView];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:72/255.0 green:105/255.0 blue:141/255.0 alpha:1.0];
    
    
   
    
   

    // Do any additional setup after loading the view from its nib.
}

- (BOOL)shouldAutorotate{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
    
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (void)setWebView{
    
    
   self.VideoWebView.delegate = self;
   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.Url]];
   [self.VideoWebView loadRequest:request];
    
    
}


-(void)setBarButtonItems{
    
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc ]initWithTitle:@"关闭" style:(UIBarButtonItemStylePlain) target:self action:@selector(didClickClose)];
    self.navigationItem.leftBarButtonItem = item;
    item.tintColor = [UIColor whiteColor];
    UIBarButtonItem *rightFavorite = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Favorite"] style:UIBarButtonItemStylePlain target:self action:@selector(favorite)];
    UIBarButtonItem *rightShare  = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(share )];
    rightShare.tintColor = [UIColor whiteColor];
    rightFavorite.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItems = @[rightShare,rightFavorite];
    
}


- (void)favorite{
    DatabaseHandle *handle =[DatabaseHandle shardInstnce];
    
    handle.fileName = @"video.sqlite3";
    handle.viewController = self;
  
    if (![handle isFavoriteModelWithID:self.model.ID]) {
        [handle insertNewactivity:self.model];
    }
    
}



- (void)share{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"55724dc967e58ee8ca002061" shareText:self.Url shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToDouban,UMShareToRenren,UMShareToSina,UMShareToEmail,UMShareToFacebook,UMShareToKakaoTalk,UMShareToFlickr, nil] delegate:nil];
}

- (void)didClickClose{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}




#pragma -----初始就加载indicatorView-------
- (void)loadIndicatorView{
    
    
    [self.view bringSubviewToFront:self.VideoIndicator];
    self.VideoIndicator.transform = CGAffineTransformMakeScale(3.0, 3.0);
    self.VideoIndicator.color = [UIColor blackColor];
    [self.VideoIndicator startAnimating];

    
//    CGPoint center = [UIApplication sharedApplication].keyWindow.center;
//    NSLog(@"%@",NSStringFromCGPoint(center));
    
    [self.VideoIndicator hidesWhenStopped];
    
     self.VideoIndicator.hidden =NO;

    
}
#pragma ---WebView的代理---
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [self.VideoIndicator stopAnimating];
    self.VideoIndicator.hidden = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    self.VideoWebView =nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
